package com.wshl.aijiuunion.controller.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.utils.BarUtils;
import com.wshl.aijiuunion.utils.SoftKeyBoardListener;
import com.wshl.aijiuunion.view.adapter.CommentAdapter;
import com.wshl.aijiuunion.view.bean.CommentListbean;
import com.wshl.aijiuunion.view.bean.VideoDetailBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class VideoDetailActivity extends AppCompatActivity implements OnLoadMoreListener {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.jz_video)
    JzvdStd jzVideo;
    @BindView(R.id.tv_video_time)
    TextView tvVideoTime;
    @BindView(R.id.tv_video_count)
    TextView tvVideoCount;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.rl_comment)
    RelativeLayout rlComment;
    @BindView(R.id.et_comment)
    EditText etComment;
    @BindView(R.id.tv_addcomment)
    TextView tvAddcomment;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R.id.recycle_comment)
    RecyclerView recycleComment;
    @BindView(R.id.refresh_videodetail)
    SmartRefreshLayout refreshVideodetail;
    @BindView(R.id.tv_nocontent)
    TextView tvNocontent;
    private SoftKeyBoardListener softKeyBoardListener;


    private RequestModel model;
    private int videoid;
    private int page = 1;
    private CommentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vieodetail);
        ButterKnife.bind(this);
        setSoftKeyBoardListener();
        BarUtils.setWhiteStatusBarForActivity(this);
        Intent intent = getIntent();
        videoid = intent.getIntExtra(GlobConstant.VIDEOID, 0);
        recycleComment.setLayoutManager(new LinearLayoutManager(this));
        model = new RequestModel();

        refreshVideodetail.setEnableRefresh(false);
        refreshVideodetail.setOnLoadMoreListener(this);
        getVideodetail(videoid);
        getCommentList(false);

    }

    private void getVideodetail(int videoid) {
        model.videoDetail(this, videoid, new RequestCallback() {
            @Override
            public void onSuccess(String response) {
                VideoDetailBean videoDetailBean = GsonUtils.fromJson(response, VideoDetailBean.class);
                VideoDetailBean.DataBean data = videoDetailBean.getData();
                String video_url = data.getVideo_url();
                String cover = data.getCover();
                String title = data.getTitle();
                String created_at = data.getCreated_at();

                jzVideo.setUp(video_url, title);
                tvVideoTime.setText(created_at + "\t" + data.getHits() + "次播放");
                tvVideoCount.setText(title);
                Glide.with(VideoDetailActivity.this).load(NetUrl.IMG + cover).into(jzVideo.thumbImageView);
            }

            @Override
            public void onError(int code) {

            }

            @Override
            public void onIntentError() {

            }
        });
    }

    @OnClick({R.id.tv_comment, R.id.img_back, R.id.tv_addcomment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                KeyboardUtils.hideSoftInput(this);
                this.finish();
                break;
//            case R.id.tv_more:
//                if (!tvMore.getText().toString().equals("查看更多")) {
//
//                    return;
//                }
//                page++;
//                getCommentList(true);
//                break;
            case R.id.tv_comment:
                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                break;
            case R.id.tv_addcomment://添加评论
                KeyboardUtils.hideSoftInput(VideoDetailActivity.this);

                String content = etComment.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showLong("请输入评论内容");
                    return;
                }
                addComment(content);
                break;
        }


    }

    private void addComment(String content) {
        model.addComment(VideoDetailActivity.this, videoid, content, new RequestCallback() {
            @Override
            public void onSuccess(String response) {
                page = 1;
                getCommentList(false);

            }

            @Override
            public void onError(int code) {

            }

            @Override
            public void onIntentError() {

            }
        });
    }

    List<CommentListbean.DataBeanX.DataBean> listdata;

    private void getCommentList(boolean isLoadmore) {
        model.getCommentlist(VideoDetailActivity.this, videoid, page, new RequestCallback() {
            @Override
            public void onSuccess(String response) {
                CommentListbean commentListbean = GsonUtils.fromJson(response, CommentListbean.class);
                CommentListbean.DataBeanX data = commentListbean.getData();
                List<CommentListbean.DataBeanX.DataBean> data1 = data.getData();
                if (isLoadmore) {
                    refreshVideodetail.finishLoadMore();
                }
                if (adapter == null) {
                    listdata = new ArrayList<>();
                    listdata.addAll(data1);
                    if (listdata.size() == 0) {
                        tvNocontent.setVisibility(View.VISIBLE);
                        recycleComment.setVisibility(View.GONE);
                        return;
                    }
                    tvNocontent.setVisibility(View.GONE);
                    recycleComment.setVisibility(View.VISIBLE);
                    adapter = new CommentAdapter(recycleComment);
                    adapter.setData(listdata);
                    recycleComment.setAdapter(adapter);
                } else if (isLoadmore) {
                    listdata.addAll(data1);
                    adapter.notifyDataSetChanged();
                } else {
                    listdata.clear();
                    listdata.addAll(data1);
                    if (listdata.size() == 0) {
                        tvNocontent.setVisibility(View.VISIBLE);
                        recycleComment.setVisibility(View.GONE);
                    }else{
                        tvNocontent.setVisibility(View.GONE);
                        recycleComment.setVisibility(View.VISIBLE);
                    }

                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onError(int code) {
                if (isLoadmore) {
                    refreshVideodetail.finishLoadMore();
                }
            }

            @Override
            public void onIntentError() {
                if (isLoadmore) {
                    refreshVideodetail.finishLoadMore();
                }
            }
        });
    }

    /**
     * 软键盘监听
     */
    private void setSoftKeyBoardListener() {
        softKeyBoardListener = new SoftKeyBoardListener(this);
        //软键盘状态监听
        softKeyBoardListener.setListener(new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                //动态设置控件宽高
                ViewGroup.LayoutParams params = rlBottom.getLayoutParams();
                rlBottom.setPadding(0, 0, 0, height);
                rlBottom.setLayoutParams(params);
                //当软键盘显示的时候
                rlBottom.setVisibility(View.VISIBLE);
                rlComment.setVisibility(View.GONE);
                //tv_shape2.setVisibility(View.VISIBLE);

                etComment.requestFocus();
                etComment.setFocusable(true);
                etComment.setCursorVisible(true);
                etComment.setFocusableInTouchMode(true);

            }

            @Override
            public void keyBoardHide(int height) {
                //当软键盘隐藏的时候
                rlBottom.setVisibility(View.GONE);
                etComment.setText("");
                rlComment.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        getCommentList(true);
    }
}
